//
//  DYForgotPasswordView.m
//  kafuMerchant
//
//  Created by 帝云科技 on 2018/9/11.
//  Copyright © 2018年 帝云科技. All rights reserved.
//

#import "DYForgotPasswordView.h"
#import "DYLoginTextFieldView.h"

@interface DYForgotPasswordView ()

@property (nonatomic,strong) DYLoginTextFieldView *phoneText;
@property (nonatomic,strong) DYLoginTextFieldView *passwordText;
@property (nonatomic,strong) DYLoginTextFieldView *codeText;
@property (nonatomic,strong) UIButton *codeButton;
@property (nonatomic,strong) UIButton *modifyButton;

@end

@implementation DYForgotPasswordView

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}
-(void)initUI
{
    [self addSubview:self.phoneText];
    [self addSubview:self.passwordText];
    [self addSubview:self.codeText];
    [self addSubview:self.codeButton];

    [self addSubview:self.modifyButton];

    
    [self.phoneText makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(BILIHEIGHT(25));
        make.left.equalTo(BILIWIDTH(18));
        make.right.equalTo(BILIWIDTH(-18));
        make.height.equalTo(BILIHEIGHT(50));
    }];
    
    [self.passwordText makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.phoneText);
        make.top.equalTo(self.phoneText.bottom).offset(BILIHEIGHT(8));
    }];

    [self.codeText makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.passwordText);
        make.top.equalTo(self.passwordText.bottom).offset(BILIHEIGHT(8));
    }];
    
    [self.codeButton makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(BILIWIDTH(-18));
        make.centerY.equalTo(self.codeText);
        make.height.equalTo(BILIHEIGHT(50));
        make.width.equalTo(BILIWIDTH(75));
    }];

    [self.modifyButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.codeText.bottom).offset(BILIHEIGHT(32));
        make.right.equalTo(self.phoneText);
        make.size.equalTo(CGSizeMake(BILIWIDTH(98), BILIHEIGHT(38)));
    }];
}
-(void)buttonClick:(UIButton *)button
{
    if (![NSString xhq_phoneFormatCheck:self.phoneText.textField.text]) {
        XHQHUDTEXT(@"请输入正确的手机号");
        return;
    }
    
    if (button==self.modifyButton) {
        
    }else if (button==self.codeButton){
        [self getTime];
    }
}

-(void)textFieldChange
{
    if (self.phoneText.textField.text.length>0&&self.passwordText.textField.text.length>0&&self.codeText.textField.text.length>0) {
        self.modifyButton.enabled=YES;
        self.modifyButton.backgroundColor=[UIColor xhq_base];
    }else{
        self.modifyButton.enabled=NO;
        self.modifyButton.backgroundColor=[UIColor xhq_default];
    }
}

-(DYLoginTextFieldView *)phoneText
{
    if (!_phoneText) {
        _phoneText=[[DYLoginTextFieldView alloc]init];
        _phoneText.textField.placeholder=@"请输入手机号";
        _phoneText.titleLabel.text=@"手机号";
        [_phoneText.textField setNumbersKeyboard];
        
        @weakify(self);
        _phoneText.textFieldChangeBlock = ^(NSString *text) {
            @strongify(self);
            [self textFieldChange];
        };
    }
    return _phoneText;
}
-(DYLoginTextFieldView *)passwordText
{
    if (!_passwordText) {
        _passwordText=[[DYLoginTextFieldView alloc]init];
        _passwordText.textField.placeholder=@"请输入密码";
        _passwordText.titleLabel.text=@"密码";
        _passwordText.isHide=NO;
        
        @weakify(self);
        _passwordText.textFieldChangeBlock = ^(NSString *text) {
            @strongify(self);
            [self textFieldChange];
        };
    }
    return _passwordText;
}
-(DYLoginTextFieldView *)codeText
{
    if (!_codeText) {
        _codeText=[[DYLoginTextFieldView alloc]init];
        _codeText.textField.placeholder=@"请输入验证码";
        _codeText.titleLabel.text=@"验证码";
        [_codeText.textField setNumbersKeyboard];
        @weakify(self);
        _codeText.textFieldChangeBlock = ^(NSString *text) {
            @strongify(self);
            [self textFieldChange];
        };
    }
    return _codeText;
}
-(UIButton *)codeButton
{
    if (!_codeButton) {
        _codeButton=[Utils createBtnWithType:UIButtonTypeCustom backgroundColor:ClearColor action:@selector(buttonClick:) target:self title:@"获取验证码" image:nil font:15 textColor:[UIColor xhq_base]];
    }
    return _codeButton;
}
-(UIButton *)modifyButton
{
    if (!_modifyButton) {
        _modifyButton=[Utils createBtnWithType:UIButtonTypeCustom backgroundColor:[UIColor xhq_default] action:@selector(buttonClick:) target:self title:@"修改" image:@"icon_login" font:17 textColor:WhiteColor];
        _modifyButton.enabled=NO;
        [_modifyButton placeImageTitlePosition:ZYButtonImagePositionRight space:BILIWIDTH(15)];
        [_modifyButton xhq_cornerRadius:BILIHEIGHT(19)];
    }
    return _modifyButton;
}
#pragma mark - 倒计时
-(void)getTime
{
    __block int timeout=59;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.codeButton setTitle:@"重新发送" forState:UIControlStateNormal];
                [self.codeButton setTitleColor:[UIColor xhq_base] forState:UIControlStateNormal];
                self.codeButton.userInteractionEnabled = YES;
            });
        }else{
            // int minutes = timeout / 60;
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.codeButton setTitle:[NSString stringWithFormat:@"%@s",strTime] forState:UIControlStateNormal];
                [self.codeButton setTitleColor:[UIColor xhq_assist] forState:UIControlStateNormal];
                self.codeButton.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
